import { Button, CheckBox, ComboBox, LineEdit, ScrollView, Slider } from "std-widgets.slint";
import { Settings } from "settings.slint";
import { Callabler } from "callabler.slint";
import { GuiState } from "gui_state.slint";
import { Translations } from "translations.slint";

// TODO use Spinbox instead LineEdit {} to be able to set only numbers

global SettingsSize {
    out property <length> item_height: 30px;
}

export component TextComponent inherits HorizontalLayout {
    in-out property <string> model;
    in property <string> name;
    spacing: 5px;
    Text {
        horizontal-stretch: 0.0;
        vertical-alignment: TextVerticalAlignment.center;
        text: name;
    }

    LineEdit {
        horizontal-stretch: 1.0;
        height: SettingsSize.item_height;
        text <=> model;
    }
}

component CheckBoxComponent inherits HorizontalLayout {
    in-out property <bool> model;
    in property <string> name;
    callback toggled();
    spacing: 5px;
    CheckBox {
        horizontal-stretch: 1.0;
        height: SettingsSize.item_height;
        checked <=> model;
        text: name;
        toggled => {
            root.toggled();
        }
    }
    Rectangle { }
}

component ThreadSliderComponent inherits HorizontalLayout {
    in-out property <float> minimum_number;
    in-out property <float> maximum_number;
    in-out property <string> name;
    spacing: 5px;

    callback changed <=> slider.changed;
    Text {
        text <=> name;
        vertical-alignment: TextVerticalAlignment.center;
        height: SettingsSize.item_height;
    }

    slider := Slider {
        enabled: true;
        height: SettingsSize.item_height;
        minimum: minimum_number;
        maximum <=> maximum_number;
        value <=> Settings.thread_number;
    }

    Text {
        height: SettingsSize.item_height;
        vertical-alignment: TextVerticalAlignment.center;
        text: round(slider.value) == 0 ? ("All (" + GuiState.maximum_threads + "/" + GuiState.maximum_threads + ")") : (round(slider.value) + "/" + GuiState.maximum_threads);
    }
}

component MinMaxSizeComponent inherits HorizontalLayout {
    spacing: 20px;
    Text {
        horizontal-stretch: 0.0;
        text <=> Translations.settings_file_size_text;
        vertical-alignment: TextVerticalAlignment.center;
    }

    HorizontalLayout {
        spacing: 5px;
        horizontal-stretch: 1.0;
        Text {
            text <=> Translations.settings_minimum_file_size_text;
            vertical-alignment: TextVerticalAlignment.center;
        }

        LineEdit {
            height: SettingsSize.item_height;
            text <=> Settings.minimum_file_size;
        }

        Text {
            text <=> Translations.settings_maximum_file_size_text;
            vertical-alignment: TextVerticalAlignment.center;
        }

        LineEdit {
            height: SettingsSize.item_height;
            text <=> Settings.maximum_file_size;
        }
    }
}

component Presets inherits Rectangle {
    property <bool> edit_name;
    property <string> current_index;
    if !edit_name: HorizontalLayout {
        spacing: 5px;
        Text {
            text <=> Translations.settings_current_preset_text;
            vertical-alignment: TextVerticalAlignment.center;
        }

        combo_box := ComboBox {
            current-index <=> Settings.settings_preset_idx;
            model: Settings.settings_presets;
            selected(item) => {
                Settings.settings_preset_idx = self.current_index;
                Callabler.changed_settings_preset();
            }
        }

        Button {
            text <=> Translations.settings_edit_name_text;
            clicked => {
                root.edit_name = !root.edit_name;
            }
        }
    }
    if edit_name: HorizontalLayout {
        spacing: 5px;
        Text {
            text: Translations.settings_choose_name_for_prefix_text + (Settings.settings_preset_idx + 1);
            vertical-alignment: TextVerticalAlignment.center;
        }

        current_name := LineEdit {
            text: Settings.settings_presets[Settings.settings_preset_idx];
        }

        Button {
            text <=> Translations.settings_save_text;
            clicked => {
                Settings.settings_presets[Settings.settings_preset_idx] = current_name.text;
                edit_name = false;
            }
        }
    }
}

component HeaderText inherits Text {
    font-size: 15px;
    height: SettingsSize.item_height;
    horizontal-alignment: TextHorizontalAlignment.center;
    vertical-alignment: TextVerticalAlignment.center;
}

component ConfigCacheButtons inherits HorizontalLayout {
    spacing: 20px;
    Button {
        text <=> Translations.settings_open_config_folder_text;
        clicked => {
            Callabler.open_config_folder();
        }
    }

    Button {
        text <=> Translations.settings_open_cache_folder_text;
        clicked => {
            Callabler.open_cache_folder();
        }
    }
}

component Languages inherits HorizontalLayout {
    spacing: 5px;
    Text {
        text <=> Translations.settings_language_text;
        vertical-alignment: TextVerticalAlignment.center;
    }

    combo_box := ComboBox {
        current_index <=> Settings.language_index;
        model <=> Settings.languages_list;
        current-value <=> Settings.language_value;

        selected(item) => {
            Callabler.changed_language();
        }
    }
}

export component SettingsList inherits VerticalLayout {
    preferred-height: 300px;
    preferred-width: 400px;

    in-out property <bool> restart_required;

    Text {
        text <=> Translations.settings_settings_text;
        height: SettingsSize.item_height;
        horizontal-alignment: TextHorizontalAlignment.center;
        font-size: 20px;
    }

    ScrollView {
        VerticalLayout {
            padding-right: 15px;
            padding-bottom: 10px;
            spacing: 5px;

            HeaderText {
                text <=> Translations.settings_global_settings_text;
            }

            Presets {
                height: SettingsSize.item_height;
            }
            Languages {
                height: SettingsSize.item_height;
            }

            CheckBoxComponent {
                name <=> Translations.settings_dark_theme_text;
                model <=> Settings.dark_theme;
                toggled => {
                    Callabler.theme_changed();
                }   
            }

            CheckBoxComponent {
                name <=> Translations.settings_show_only_icons_text;
                model <=> Settings.show_only_icons;
            }
    
            CheckBoxComponent {
                name <=> Translations.settings_load_windows_size_at_startup_text;
                model <=> Settings.load_windows_size_at_startup;
            }
            
            CheckBoxComponent {
                name <=> Translations.settings_load_tabs_sizes_at_startup_text;
                model <=> Settings.load_tabs_sizes_at_startup;
            }

            HeaderText {
                text <=> Translations.settings_general_settings_text;
            }

            TextComponent {
                name <=> Translations.settings_excluded_items_text;
                model <=> Settings.excluded_items;
            }

            TextComponent {
                name <=> Translations.settings_allowed_extensions_text;
                model <=> Settings.allowed_extensions;
            }

            TextComponent {
                name <=> Translations.settings_excluded_extensions_text;
                model <=> Settings.excluded_extensions;
            }

            MinMaxSizeComponent { }

            CheckBoxComponent {
                name <=> Translations.settings_recursive_search_text;
                model <=> Settings.recursive_search;
            }

            CheckBoxComponent {
                name <=> Translations.settings_use_cache_text;
                model <=> Settings.use_cache;
            }

            CheckBoxComponent {
                name <=> Translations.settings_save_as_json_text;
                model <=> Settings.save_as_json;
            }

            CheckBoxComponent {
                name <=> Translations.settings_move_to_trash_text;
                model <=> Settings.move_to_trash;
            }

            CheckBoxComponent {
                name <=> Translations.settings_ignore_other_filesystems_text;
                model <=> Settings.ignore_other_filesystems;
            }

            ThreadSliderComponent {
                name <=> Translations.settings_thread_number_text;
                maximum_number <=> GuiState.maximum_threads;
                changed => {
                    restart_required = true;
                }
            }

            if restart_required: Text {
                text <=> Translations.settings_restart_required_text;
                horizontal-alignment: TextHorizontalAlignment.center;
            }
            HeaderText {
                text <=> Translations.tool_duplicate_files_text;
            }

            CheckBoxComponent {
                name <=> Translations.settings_duplicate_image_preview_text;
                model <=> Settings.duplicate_image_preview;
            }

            CheckBoxComponent {
                name <=> Translations.settings_duplicate_hide_hard_links_text;
                model <=> Settings.duplicate_hide_hard_links;
            }

            TextComponent {
                name <=> Translations.settings_duplicate_minimal_hash_cache_size_text;
                model <=> Settings.duplicate_minimal_hash_cache_size;
            }

            CheckBoxComponent {
                name <=> Translations.settings_duplicate_use_prehash_text;
                model <=> Settings.duplicate_use_prehash;
            }

            TextComponent {
                name <=> Translations.settings_duplicate_minimal_prehash_cache_size_text;
                model <=> Settings.duplicate_minimal_prehash_cache_size;
            }

            CheckBoxComponent {
                name <=> Translations.settings_duplicate_delete_outdated_entries_text;
                model <=> Settings.duplicate_delete_outdated_entries;
            }

            HeaderText {
                text <=> Translations.settings_similar_images_tool_text;
            }

            CheckBoxComponent {
                name <=> Translations.settings_similar_images_show_image_preview_text;
                model <=> Settings.similar_images_show_image_preview;
            }

            CheckBoxComponent {
                name <=> Translations.settings_similar_images_hide_hard_links_text;
                model <=> Settings.similar_images_hide_hard_links;
            }

            CheckBoxComponent {
                name <=> Translations.settings_delete_outdated_entries_text;
                model <=> Settings.similar_images_delete_outdated_entries;
            }

            HeaderText {
                text <=> Translations.settings_similar_videos_tool_text;
            }

            CheckBoxComponent {
                name <=> Translations.settings_similar_videos_hide_hard_links_text;
                model <=> Settings.similar_videos_hide_hard_links;
            }

            CheckBoxComponent {
                name <=> Translations.settings_delete_outdated_entries_text;
                model <=> Settings.similar_videos_delete_outdated_entries;
            }

            HeaderText {
                text <=> Translations.settings_similar_music_tool_text;
            }

            CheckBoxComponent {
                name <=> Translations.settings_delete_outdated_entries_text;
                model <=> Settings.similar_music_delete_outdated_entries;
            }

            ConfigCacheButtons { }
        }
    }

    HorizontalLayout {
        spacing: 5px;
        Button {
            text <=> Translations.settings_save_text;
            clicked => {
                Callabler.save_current_preset();
            }
        }

        Button {
            text <=> Translations.settings_load_text;
            clicked => {
                Callabler.load_current_preset();
            }
        }

        Button {
            text <=> Translations.settings_reset_text;
            clicked => {
                Callabler.reset_current_preset();
            }
        }
    }
}
